package com.njbw.user.controller;

import com.njbw.redis.util.CacheUtil;
import com.njbw.redis.util.RedisShareLockUtil;
import com.njbw.redis.util.RedisUtil;
import com.njbw.tool.ExportWordUtil;
import com.njbw.user.entity.req.SysUserReq;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Log4j2
@RestController
public class TestController {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RedisShareLockUtil redisShareLockUtil;

    @Resource
    private CacheUtil cacheUtil;

    /*@Resource
    private MinioTemplate minioTemplate;*/

    @GetMapping("/testRedisLock")
    public void testRedisLock() {
        boolean jichi = redisShareLockUtil.lock("jichi", "1231231", 10000L);
        System.out.println(jichi);
    }

    @GetMapping("/test")
    public String test() {
        return "Hello World";
    }

    @GetMapping("/testRedis")
    public void testRedis() {
        redisTemplate.opsForValue().set("name", "经典鸡翅");
    }

    @GetMapping("/testRedisUtil")
    public void testRedisUtil() {
        redisUtil.set("name", "经典鸡翅");
    }

    @GetMapping("/testLog")
    public void testLog() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            log.info("这是 {} 条日志", i);
        }
        long end = System.currentTimeMillis();
        log.info("当前打印日志耗时：{}", end - start);
    }

    @GetMapping("/testExport")
    public void testExport() throws Exception {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", "南迦巴瓦");
        dataMap.put("auditName", "他年他翁");
        ExportWordUtil.exportWord(dataMap, "导出文件", "wordExport.ftl");
    }

    @PostMapping("/testQuery")
    public void testQuery(@RequestBody SysUserReq sysUserReq) {
        // 2022-12-18 21:49:00
        System.out.println(sysUserReq);
    }

    /*@GetMapping("/testMinio")
    public CreateMultipartUploadResult getUploadId() throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, IOException, InvalidKeyException, XmlParserException, InvalidResponseException, InternalException {
        FileMultipartUploadReq req = new FileMultipartUploadReq.Builder()
                .objectName("njbw/uuid.mp4")
                .chunkSize(6)
                .build();
        return minioTemplate.createMultipartUpload(req);
    }*/

    @GetMapping("/testLocalCache")
    public void testLocalCache() throws Exception {
        // list<long> skuId
        // 价格。商品名称。优惠券。限购 批量
        // Map<Long, SkuInfo>
        // 反射
        List<Long> skuIdList = new ArrayList<>();
        cacheUtil.getResult(skuIdList, "skuInfo.skuName", SkuInfo.class, (list) -> {
            Map<Long, SkuInfo> skuInfoMap = getSkuName((List<Long>) list);
            return skuInfoMap;
        });

        cacheUtil.getResult(skuIdList, "skuInfo.skuPrice", SkuPriceInfo.class, (list) -> {
            Map<Long, SkuPriceInfo> skuPriceInfoMap = getSkuPrice((List<Long>) list);
            return skuPriceInfoMap;
        });
    }


    public Map<Long, SkuInfo> getSkuName(List<Long> skuIds) {
        return Collections.emptyMap();
    }

    public Map<Long, SkuPriceInfo> getSkuPrice(List<Long> skuIds) {
        return Collections.emptyMap();
    }

    @Data
    class SkuInfo {

        private Long id;

        private String name;

        private Long price;
    }

    @Data
    class SkuPriceInfo {

        private Long id;

        private String name;

        private Long price;
    }
}
